#include "global.h"
#include "ship.h"
#include "blackhole.h"
#include "asteroid.h"
#include "projectile.h"

volatile int ticks = 0;
void ticker()
{
        ticks++;
}
END_OF_FUNCTION(ticker)

class ship;
class projectile;
class asteroid;
class powerup;
class blackhole;

SAMPLE* shoot_sound = load_wav("shoot1.wav");
SAMPLE* splode_sound = load_wav("splode1.wav");
SAMPLE* crash_sound = load_wav("crash1.wav");
SAMPLE* hit_roid = load_wav("hitroid1.wav");
SAMPLE* laser_sound = load_wav("laser.wav");
SAMPLE* pickup_sound = load_wav("pickup.wav");
BITMAP* buffer;
int proj_spd = 3;
int powerup_count = 0;
int win = 0;
int lose = 0;
int bullet_fired = 0;
int max_asteroids = 3;
int score = 0;
int max_powerup = 1;
int bulletpower = 1;
int spreadpower = 1;
int game_over = 0;
int asteroid_count = 0;
//----------------------------------------------------------------------------------------------------------
void draw_cur_proj_indicator(BITMAP* buffer, ship player, projectile& bullet, projectile* spread[], projectile& laser){
    int bulletcolor = WHITE;
    int spreadcolor = WHITE;
    int lasercolor  = WHITE;

//SET COLOR FOR BULLET INDICATOR BASED UPON POWER OF PROJECTILE
    if(bullet.get_power() == 1)
        bulletcolor = RED;
    else if(bullet.get_power() == 2)
        bulletcolor = YELLOW;
    else if(bullet.get_power() == 3)
        bulletcolor = GREEN;
    else
        bulletcolor = WHITE;

//SET COLOR FOR SPREADSHOT INDICATOR BASED UPON POWER OF PROJECTILE
    if(spread[0]->get_power() == 1)
        spreadcolor = RED;
    else if(spread[0]->get_power() == 2)
        spreadcolor = YELLOW;
    else if(spread[0]->get_power() == 3)
        spreadcolor = GREEN;
    else
        spreadcolor = WHITE;
//SET COLOR FOR LASER INDICATOR BASED UPON POWER OF LASER
    lasercolor = GREEN;

//DRAW SINGLE_SHOT BOX
    if(player.get_cur_proj() == 0){
        rect(buffer, SCREEN_W*.85, SCREEN_H*.055,  SCREEN_W*.8775, SCREEN_H*.0895, bulletcolor);  //SELECTED single_shot box
        circle(buffer, SCREEN_W*.86375, SCREEN_H*.07225, 2, bulletcolor); //SELECTED single_shot icon
    }
    else{
        rect(buffer, SCREEN_W*.85, SCREEN_H*.055,  SCREEN_W*.8775, SCREEN_H*.0895, WHITE);  //NON_SELECTED single_shot box
        circle(buffer, SCREEN_W*.86375, SCREEN_H*.07225, 2, WHITE); //NON_SELECTED single_shot icon
    }

//DRAW SPREADSHOT BOX
    if(player.get_cur_proj() == 1){
        rect  (buffer, SCREEN_W*.881, SCREEN_H*.055, SCREEN_W*.9085, SCREEN_H*.0895, spreadcolor);  //SELECTED spreadshot box
        circle(buffer, SCREEN_W*.89475, SCREEN_H*.07225, 2, spreadcolor); //SELECTED spread0 icon
        circle(buffer, SCREEN_W*.887875, SCREEN_H*.080875, 2, spreadcolor); //SELECTED spread1 icon
        circle(buffer, SCREEN_W*.901625, SCREEN_H*.063625, 2, spreadcolor); //SELECTED spread2 icon
        draw_num(player.get_proj_count(player.get_cur_proj()), SCREEN_W*.953, SCREEN_H*.055, SCREEN_W*.0127, spreadcolor);
    }
    else{
        rect  (buffer, SCREEN_W*.881, SCREEN_H*.055, SCREEN_W*.9085, SCREEN_H*.0895, WHITE);  //NON_SELECTED spreadshot box
        circle(buffer, SCREEN_W*.89475, SCREEN_H*.07225, 2, WHITE); //NON_SELECTED spread0 icon
        circle(buffer, SCREEN_W*.887875, SCREEN_H*.080875, 2, WHITE); //NON_SELECTED spread1 icon
        circle(buffer, SCREEN_W*.901625, SCREEN_H*.063625, 2, WHITE); //NON_SELECTED spread2 icon
    }

//DRAW LASER BOX
    if(player.get_cur_proj() == 2){
        rect(buffer, SCREEN_W*.913, SCREEN_H*.055, SCREEN_W*.9395, SCREEN_H*.0895, lasercolor);  //SELECTED spreadshot box
        line(buffer, SCREEN_W*.919625, SCREEN_H*.080875, SCREEN_W*.932875, SCREEN_H*.063625, lasercolor); //laser icon
        draw_num(player.get_proj_count(player.get_cur_proj()), SCREEN_W*.953, SCREEN_H*.055, SCREEN_W*.0127, lasercolor);
    }
    else{
        rect(buffer, SCREEN_W*.913, SCREEN_H*.055, SCREEN_W*.9395, SCREEN_H*.0895, WHITE);  //projectile 3 box
        line(buffer, SCREEN_W*.919625, SCREEN_H*.080875, SCREEN_W*.932875, SCREEN_H*.063625, WHITE); //laser icon
    }
}

void draw_ship_count(BITMAP* buffer, int shipcount){
    line(buffer, SCREEN_W*.036, SCREEN_H*.0550, SCREEN_W*.026, SCREEN_H*.0800 , GREEN);
    line(buffer, SCREEN_W*.036, SCREEN_H*.0550, SCREEN_W*.046, SCREEN_H*.0800 , GREEN);
    line(buffer, SCREEN_W*.026, SCREEN_H*.0800, SCREEN_W*.046, SCREEN_H*.0800 , GREEN);
    draw_num(shipcount, SCREEN_W*.062, SCREEN_H*.0550, SCREEN_W*.009, GREEN);
}

void draw_screen(BITMAP* buffer, ship player, projectile& bullet, projectile* spread[], projectile& laser, asteroid* roids[], blackhole& hole, powerup* pow, int score){

//DRAW INTERFACE
    rect(buffer, SCREEN_W*.02, SCREEN_H*.05, SCREEN_W-(SCREEN_W*.02), SCREEN_H-(SCREEN_H*.02), WHITE);
    draw_cur_proj_indicator(buffer, player, bullet, spread, laser);

//DRAW GAME OBJECTS
    hole.draw();
    player.draw(buffer);
    pow->draw();
    player.display_energy(buffer);

    if(bullet.get_exists()){
        bullet.draw(buffer, bullet.get_type());
    }
    if(laser.get_exists()){
        laser.draw(buffer, laser.get_type());
    }
    for (int i = 0; i < 3; i++){
        if(spread[i]->get_exists()){
            spread[i]->draw(buffer, spread[i]->get_type());
        }
    }
    for (int i = 0; i < 3; i++){
        if(roids[i]->get_exists()){
            roids[i]->draw(buffer);
        }
    }
    draw_ship_count(buffer, player.get_lives());
    draw_num(score, xcenter, SCREEN_H*.055, 10, makecol(255, 0, 0));

    blit(buffer, screen, 0, 0, 0, 0, buffer->w, buffer->h);
    clear_to_color(buffer, makecol(0,0,0));
}

void init() {
	int depth, res;
    srand(time(NULL));

	allegro_init();
	depth = desktop_color_depth();

    if(install_sound(DIGI_AUTODETECT, MIDI_AUTODETECT, NULL)) {
        allegro_message("Sound Error: %s", allegro_error);
        exit(-1);
    }


    if (depth == 0) depth = 16;
	set_color_depth(depth);
	//res = set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 1680, 1050, 0, 0);
	res = set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 1024, 768, 0, 0);
    //res = set_gfx_mode(GFX_AUTODETECT_FULLSCREEN, 640, 480 , 0, 0);
    if (res != 0) {
		allegro_message(allegro_error);
		exit(-1);
	}

	install_timer();
	install_keyboard();
	install_mouse();
    set_keyboard_rate(1000, 1000);
	buffer = create_bitmap(SCREEN_W, SCREEN_H);
    set_clip_rect(buffer, SCREEN_W*.02, SCREEN_H*.05, SCREEN_W-(SCREEN_W*.02), SCREEN_H-(SCREEN_H*.02)); /* disable drawing on bmp */
    clear_to_color(buffer, makecol(0, 0, 0));
}


void deinit() {
	clear_keybuf();
	destroy_bitmap(buffer);
}
void draw_digit(int n, int x, int y, int size, int color){
 //10 digits, 7 "bits" per display
    int num[10][7] = {1, 1, 1, 1, 1, 1, 0,  //bitmap for 0
                     0, 1, 1, 0, 0, 0, 0,   //bitmap for 1
                     1, 1, 0, 1, 1, 0, 1,   //bitmap for 2
                     1, 1, 1, 1, 0, 0, 1,   //bitmap for 3
                     0, 1, 1, 0, 0, 1, 1,   //bitmap for 4
                     1, 0, 1, 1, 0, 1, 1,   //bitmap for 5
                     1, 0, 1, 1, 1, 1, 1,   //bitmap for 6
                     1, 1, 1, 0, 0, 0, 0,   //bitmap for 7
                     1, 1, 1, 1, 1, 1, 1,   //bitmap for 8
                     1, 1, 1, 1, 0, 1, 1};  //bitmap for 9
    if(num[n][0]) {
        line(buffer, x, y, x+size, y, color);
    }
    if(num[n][1]) {
        line(buffer, x+size, y, x+size, y+size, color);
    }
    if(num[n][2]) {
        line(buffer, x+size, y+size, x+size, y+(size*2), color);
    }
    if(num[n][3]) {
        line(buffer, x, y+(size*2), x+size, y+(size*2), color);
    }
    if(num[n][4]) {
        line(buffer, x, y+size, x, y+(size*2), color);
    }
    if(num[n][5]) {
        line(buffer, x, y, x, y+size, color);
    }
    if(num[n][6]) {
        line(buffer, x, y+size, x+size, y+size, color);
    }
}


void draw_num(unsigned long num, int x, int y, int size, int color){
    int numsize = log10(num)+1;  // DETERMINE NUMBER OF PLACES IN THE NUMBER
    if(numsize==0){
        numsize = 1;
    }
    int numarray[numsize];
    for (int i = 0; i < numsize; ++i, num /= 10 )
    {
        numarray[i] = num % 10;  //FILL THE ARRAY WITH EACH DIGIT IN THE NUMBER
    }

    for (int i = 0; i < numsize; i++){
        int xloc = x + (numsize/2 * 10) - SCREEN_W*.020*i;  // CREATE THE PROPER OFFSET FOR CENTER_X DISPLAY
        draw_digit(numarray[i], xloc, y, size, color);
    }

}
